<title>编辑消息</title>

<div class="layui-fluid">  
  <div class="layui-row layui-col-space30" style="margin-top: 0px;">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <div class="LAY_wisdom_title">
        <h3 class="LAY_wisdom_title_line">基本信息</h3>
      </div>

      <form class="layui-form" lay-filter="LAY_message_form">
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">消息主题</label>
          <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" placeholder="请输入" maxlength="64" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">推送时间</label>
          <div class="layui-input-block">
            <input type="text" name="effectTime" lay-verify="required|time" id="LAY_time" readonly placeholder="请选择" autocomplete="off" class="layui-input">
          </div>
        </div>
        
        <button style="display: none;" id="LAY_message_form_submit" lay-submit lay-filter="*">提交</button>
      </form>
      
      <div class="LAY_wisdom_title">
        <h3 class="LAY_wisdom_title_line">消息内容</h3>
      </div>

      <div class="layui-form-item layui-form-text">
        <div class="layui-input-block">
          <textarea class="layui-textarea" id="LAY_textarea" style="display: none"></textarea>
        </div>
      </div>
    </div>
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <div class="LAY_wisdom_title">
        <h3 class="LAY_wisdom_title_line">目标机构</h3>
      </div>

      <table id="LAY_message_form_org" lay-filter="LAY_message_form_org"></table>
    </div>
  </div>
</div>

<style>
  html {
    background-color: #fff;
  }

  body.layui-layout-body {
    overflow-y: auto;
  }
</style>

<script>
  layui.use(['admin', 'form', 'laytpl', 'table', 'layer', 'laydate', 'layedit', 'util'], function(){
    var $ = layui.$,
    admin = layui.admin,
    layer = layui.layer,
    setter = layui.setter,
    form = layui.form,
    table = layui.table,
    laydate = layui.laydate,
    util = layui.util,
    layedit = layui.layedit,
    router = layui.router(),
    messageId = router.search.messageId;

    window.getData = getData;

    var today = new Date().getTime();
    var tomorrow = new Date().getTime()+24*60*60*1000;
    var markDateValue = util.toDateString(new Date(), 'yyyy-MM-dd') + ' 10:00:00';
    var mark = new Date(markDateValue).getTime();

    if (today > mark) {
      markDateValue = util.toDateString(tomorrow, 'yyyy-MM-dd') + ' 10:00:00';
    }

    laydate.render({
      elem: '#LAY_time',
      type: 'datetime',
      value: markDateValue,
      format: 'yyyy-MM-dd HH:mm:ss'
    });

    form.render(null, 'LAY_message_form');

    form.verify({
      time: function (value, item) {
        var targetTime = new Date(value).getTime();
        var currentTime = new Date().getTime();

        if (targetTime < currentTime) {
          return '消息生效推送时间不能早于当前时间';
        }
      }
    });

    var layeditIndex = layedit.build('LAY_textarea', {
      tool: [
        'strong' //加粗
        ,'italic' //斜体
        ,'underline' //下划线
        ,'del' //删除线
        
        ,'|' //分割线
        
        ,'left' //左对齐
        ,'center' //居中对齐
        ,'right' //右对齐
        ,'link' //超链接
        ,'unlink' //清除链接
      ]
    });
    
    if (messageId == 'undefined') {
      initTable()
    } else {
      render();
    }

    form.on('submit(*)', function () {
      return false;
    });

    function getData() {
      // 获取表单
      var content = layedit.getContent(layeditIndex);
      var feild = form.val('LAY_message_form');

      // 获取机构
      var checkedItems = table.checkStatus('LAY_message_form_org').data;
      var deliveryTargets = [];
      
      layui.each(checkedItems, function (index, item) {
        deliveryTargets.push(item.orgCode);
      });
      
      deliveryTargets = deliveryTargets.toString();

      // 组合数据
      var params = $.extend(true, {}, feild, {
        content: content,
        deliveryTargets: deliveryTargets,
      });

      return params;
    }

    // 回填信息
    function render() {
      admin.req({
        url: setter.url + '/message/detail',
        type: 'GET',
        data: { messageId: messageId },
        done: function (res) {
          var data = res.data || [];
          
          // 回填form
          form.val('LAY_message_form', {
            title: data.title,
            effectTime: data.effectTime
          });

          // 回填edit
          layedit.setContent(layeditIndex, data.content);

          // 回填table的checkbox
          initTable(data.existsDeliveryTargets);
        }
      });
    }

    function initTable(existsDeliveryTargets) {
      table.render({
        elem: '#LAY_message_form_org'
        ,url: setter.url + '/org/liteList' //模拟接口
        ,page: false
        ,limit: Number.MAX_VALUE
        ,height: 468
        ,parseData: function(res){
          if (existsDeliveryTargets) {
            for(var i in res.data){
              if(existsDeliveryTargets.indexOf(res.data[i].orgCode) > -1){
                res.data[i]["LAY_CHECKED"] = true;
              }
            }
          }

          return {
            "code": res.code,
            "msg": res.msg,
            "data": res.data
          };
        }
        ,response: {
          statusCode: 200 //规定成功的状态码，默认：0
        }
        ,cols: [[
          {type: 'checkbox', align: 'center'}
          ,{ field: 'orgSimpleName', align: 'center', title: '机构简称' }
          ,{ field: 'parentOrgSimpleName',align: 'center', title: '上级机构简称' }
          ,{ field: 'orgCategoryDesc',align: 'center', title: '机构类型' }
        ]]
      });
    }

  });
</script>
